package ogltest1;

import rctgl.RCTGLViewer;

import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;


import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class TestRenderer implements GLEventListener, KeyListener
{
    private RCTGLViewer m_viewer = new RCTGLViewer();
    
    private GLU glu = new GLU();


    public void init(GLAutoDrawable drawable)
    {
    	GL gl = drawable.getGL();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and 
                                       // see what happens.
        
        drawable.addKeyListener(this);
        
    }
    
    

    public void display(GLAutoDrawable drawable)
    {
    	
    	final GL gl = drawable.getGL();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        
        double [] coords = m_viewer.getPosition();
        gl.glTranslated(-1.5f + coords[0], coords[1], -6.0f + coords[2]);
        
        
        gl.glPushMatrix();
        
        //gl.glTranslatef(-1.5f, 0.0f, -6.0f);
        gl.glBegin(GL.GL_TRIANGLES);        // Drawing Using Triangles
        gl.glColor3f(1.0f, 0.0f, 0.0f);   // Set the current drawing color to red
        gl.glVertex3f(0.0f, 1.0f, 0.0f);  // Top
        gl.glColor3f(0.0f, 1.0f, 0.0f);   // Set the current drawing color to green
        gl.glVertex3f(-1.0f, -1.0f, 0.0f);  // Bottom Left
        gl.glColor3f(0.0f, 0.0f, 1.0f);   // Set the current drawing color to blue
        gl.glVertex3f(1.0f, -1.0f, 0.0f);  // Bottom Right
        gl.glEnd();        // Finished Drawing The Triangle
        gl.glTranslatef(3.0f, 0.0f, 0.0f);
        gl.glBegin(GL.GL_QUADS);             // Draw A Quad
        gl.glColor3f(0.5f, 0.5f, 1.0f);   // Set the current drawing color to light blue
        gl.glVertex3f(-1.0f, 1.0f, 0.0f);  // Top Left
        gl.glVertex3f(1.0f, 1.0f, 0.0f);  // Top Right
        gl.glVertex3f(1.0f, -1.0f, 0.0f);  // Bottom Right
        gl.glVertex3f(-1.0f, -1.0f, 0.0f);  // Bottom Left
        gl.glEnd();        // Done Drawing The Quad
        
        gl.glPopMatrix();
        
        gl.glFlush();
        
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
    {
    	final GL gl = drawable.getGL();

        if (height <= 0) // avoid a divide by zero error!
            height = 1;
        final float h = (float) width / (float) height;
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, h, 0.1f, 1000.0);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
        
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
    {
    }

	public void keyPressed(KeyEvent e)
	{
		System.out.println("got key pressed " + e.toString());
		
		switch(e.getKeyCode())
		{
		case KeyEvent.VK_ESCAPE:
			System.exit(0);
		case KeyEvent.VK_W:
			m_viewer.moveRelative(RCTGLViewer.MOVEUNIT, 0, 0);
			break;
		case KeyEvent.VK_S:
			m_viewer.moveRelative(-RCTGLViewer.MOVEUNIT, 0, 0);
			break;
		case KeyEvent.VK_D:
			m_viewer.moveRelative(0, 0, RCTGLViewer.MOVEUNIT);
			break;
		case KeyEvent.VK_A:
			m_viewer.moveRelative(0, 0, -RCTGLViewer.MOVEUNIT);
			break;
		case KeyEvent.VK_PAGE_UP:
			m_viewer.moveRelative(0, RCTGLViewer.MOVEUNIT, 0);
			break;
		case KeyEvent.VK_PAGE_DOWN:
			m_viewer.moveRelative(0, -RCTGLViewer.MOVEUNIT, 0);
			break;
		}
		
		
		
	}

	public void keyReleased(KeyEvent e)
	{
		//System.out.println("got key released " + e.toString());
		
		
		
	}

	public void keyTyped(KeyEvent e)
	{
		//System.out.println("got key typed " + e.toString());
		
		
		
	}
    
    
    
    
	
}
